<?php

namespace src\DataBase;
require_once __DIR__ . '/../../autoload.php';

class DataBaseOperate extends Config
{

    public function table($table)
    {
        return $this->table = $table;
    }

    public function field($field = '*')
    {
        return $this->field = $field;
    }

    public $sqlName;
    //插入数据形式为:array('roomID'=>'301','studentInfo'=>'S1601081128','courseName'=>'RC震荡电路')
    //返回数据为:(roomID,studentInfo,courseName) values ('301','S1601081128','RC震荡电路')
    public function insertArrayToString(array $data)
    {
        $count = count($data);
        $field = implode(",", array_keys($data));
        $valuesArray = array_values($data);
        for ($i = 0; $i < $count - 1; $i++) {
            $valuesString .= sprintf("'%s'", $valuesArray[$i]) . ',';
        }
        $valuesString .= sprintf("'%s'", $valuesArray[$count - 1]);
        $return = "($field)" . ' values ' . "($valuesString)";
        return $return;
    }

    /*查询条件(
    单条件查询:array('studentNumber'=>'s1601081128')  =>  studentNumber='s1601081128'
    多条件查询:array('studentNumber'=>'s1601081128','password'='MCX857264')   =>
    studentNumber='s1601081128' and password='MCX857264'
    多字符插入:array(1,2,3,4,5)  =>  1,2,3,4,5
     )
    */
    public function where($where = array())
    {
        return $this->where = (' where ' . ($this->arrayToString($where)));
    }

    public function order(string $order)
    {
        return $this->order = $order;
    }

    public function limit(int $limit)
    {
        return $this->limit = $limit;
    }

    /*条件查询单条数据或按某个字段在数据库中查询该字段的所有数据
	$data = new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
	单条件查询：$select=$data->select('studentInfo','password',array('studentNumber'=>'S1601081128'));
	var_dump($select);
	echo "</br>";
	按某个字段查询数据库所有该字段的所有数据：$select1=$data->select('studentInfo','studentNumber');
	var_dump($select1);
	 $field 一直为 '*'
	 desc:默认为降序排列
	*/
    public function select($table, $field = '', $where = [], $order = '', $limit = '')
    {
        $this->init = $this->init();
        if (is_object($this->init)) {
            if (!empty($where)) {
                if (!empty($order)) {
                    if (!empty($limit)) {
                        $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table) . $this->where($where) . ' order by ' . $this->order($order) . ' desc ' . ' limit ' . $this->limit($limit);
                        $this->sqlName = $sql;
                        $result = mysqli_query($this->init, $sql);
                        if (!($result)) {
                            echo $this->msg = '数据库查询失败!';
                        } else {
                            while ($row = mysqli_fetch_all($result)) {
                                return $row;
                                mysqli_close($this->init);
                            }
                        }

                    } else {
                        $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table) . $this->where($where) . ' order by ' . $this->order($order) . ' desc';
                        $this->sqlName = $sql;
                        $result = mysqli_query($this->init, $sql);
                        if (!($result)) {
                            echo $this->msg = '数据库查询失败!';
                        } else {
                            while ($row = mysqli_fetch_all($result)) {
                                return $row;
                                mysqli_close($this->init);
                            }
                        }
                    }

                } else {
                    $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table) . $this->where($where);
                    $this->sqlName = $sql;
                    $result = mysqli_query($this->init, $sql);
                    if (!($result)) {
                        echo $this->msg = '数据库查询失败!';
                    } else {
                        while ($row = mysqli_fetch_assoc($result)) {
                            return $row;
                            mysqli_close($this->init);
                        }
                    }
                }

            } else {
                if (!empty($order)) {
                    if (!empty($limit)) {
                        $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table) . ' order by ' . $this->order($order) . ' desc ' . ' limit ' . $this->limit($limit);
                        $this->sqlName = $sql;
                        $result = mysqli_query($this->init, $sql);
                        if (!($result)) {
                            echo $this->msg = '数据库查询失败!';
                        } else {
                            while ($row = mysqli_fetch_all($result)) {
                                return $row;
                                mysqli_close($this->init);
                            }
                        }

                    } else {
                        $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table) . ' order by ' . $this->order($order) . ' desc';
                        $this->sqlName = $sql;
                        $result = mysqli_query($this->init, $sql);
                        if (!($result)) {
                            echo $this->msg = '数据库查询失败!';
                        } else {
                            while ($row = mysqli_fetch_all($result)) {
                                return $row;
                                mysqli_close($this->init);
                            }
                        }
                    }
                } else {
                    $sql = 'select ' . $this->field($field) . ' from ' . $this->table($table);
                    $this->sqlName = $sql;
                    $result = mysqli_query($this->init, $sql);
                    if (!($result)) {
                        echo $this->msg = '数据库查询失败!';
                    } else {
                        while ($row = mysqli_fetch_all($result)) {
                            return $row;
                            mysqli_close($this->init);
                        }
                    }

                }
            }

        } else {
            return $this->init;
        }
    }
    /**
     * @param $table
     * @param array $data
     * @return bool|\mysqli|void|null
     * $data=array('password'=>'MCX857264');
     */
    public function insert($table, array $data)
    {
        $this->init = $this->init();
        if (is_object($this->init)) {
            $insertArrayToString = $this->insertArrayToString($data);
            $sql = 'insert into ' . $this->table($table) . ' ' . $insertArrayToString;
            $this->sqlName = $sql;
            $result = mysqli_query($this->init, $sql);
            if (!($result)) {
                echo $this->msg = '数据库插入失败!';
            } else {
                return true;
                mysqli_close($this->init);
            }
        } else {
            return $this->init;
        }
    }

    public function update($table, $file, $where)
    {
        $this->init = $this->init();
        if (is_object($this->init)) {
            $sql = 'update ' . $this->table($table) . ' set ' . $this->arrayToString($file) . $this->where($where);
            $this->sqlName = $sql;
            $result = mysqli_query($this->init, $sql);
            if (!($result)) {
                echo $this->msg = '密码更新失败!';
            } else {
                return $this->sqlName;
                mysqli_close($this->init);
            }
        } else {
            return $this->init;
        }
    }

    //单条数据删除
    public function deleteData($table, $where)
    {
        $this->init = $this->init();
        if (is_object($this->init)) {
            $sql = 'delete from ' . $this->table($table) . $this->where($where);
            $result = mysqli_query($this->init, $sql);
            if (!$result) {
                echo $this->msg = '数据库删除失败!';
            } else {
                return true;
                mysqli_close($this->init);
            }
        } else {
            return $this->init;
        }
    }


    public function index()
    {
        echo "数据库操作类加载成功";
    }
}

/* $db= new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
$selectResult=$db->update('studentInfo',array('password'=>'S1601081128'),array('studentNumber'=>'S1601081128'));
$sql=$db->sqlName;
var_dump($sql);
echo "</br>";
var_dump($selectResult); */


/* $db= new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
$selectResult=$db->select('courseSelectionRecord','*',array('studentInfo'=>'S1601081128'),'insertTime',6);
$sql=$db->sqlName;
var_dump($sql);
echo "</br>";
var_dump($selectResult); */
/* $data = new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
$field= $data->insert('courseSelectionRecord',array('roomID'=>'301','studentInfo'=>'S1601081128','courseName'=>'RC震荡电路','courseTime'=>'第一节课','insertTime'=>'2018-07-02 11:00:00'));
var_dump ($field); */

/* var_dump(array_keys($field));
echo "</br>";
var_dump(implode(",",array_keys($field)));
echo "</br>";
$array=array_values($field);
var_dump($array);
echo "</br>";

 $count=count($array);
for($i=0;$i<$count-1;$i++){
	$string .= sprintf("'%s'",$array[$i]).',';
}
$string .= sprintf("'%s'",$array[$count-1]);
var_dump($string); */

/* $data = new DataBaseOperate('139.196.124.180','school','user1','Mcx857264','utf8');
$select=$data->select('studentInfo','*',array('studentNumber'=>'S1601081128','password'=>'Mcx857264'));
echo( $select['studentNumber']);
if(isset($select)){
	echo "success";
}else{
	echo "false";
} */

/* var_dump($data->arrayToString(array(1,2,3,4)));
echo "</br>";
var_dump($data->where(array('studentNumber'=>'s1601081128')));
echo "</br>";
var_dump($data->where(array('studentNumber'=>'s1601081128','Id'=>'12'))); */
?>